package com.skillworld.webapp.web.pages.rest.school;

import java.util.List;

import org.apache.tapestry5.annotations.ContentType;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;

import com.skillworld.webapp.model.courseservice.CourseService;
import com.skillworld.webapp.model.courseservice.NotAMasterOfCourseException;
import com.skillworld.webapp.model.lesson.Lesson;
import com.skillworld.webapp.web.util.ErrorMessages;
import com.skillworld.webapp.web.util.Parameter;
import com.skillworld.webapp.web.util.UserSession;

import es.udc.pojo.modelutil.exceptions.InstanceNotFoundException;

@ContentType("text/xml")
public class RemoveLesson {

	@SuppressWarnings("unused")
    @Property
    private String error_string = null;
    
    @SuppressWarnings("unused")
    @SessionState(create=false)
    private UserSession userSession;
   
    private boolean userSessionExists;
    
    @Inject
    private CourseService courseService;

    @Property
    private Long lessonId;
    
    @Inject
    private Request request;
    	
    void onPassivate() {
        return;
    }
	
    void onActivate()
    {
        String lessonId_string=null;
        //
        
        if (!userSessionExists)
        {
            error_string = ErrorMessages.NOT_LOGGED_IN;
            return;
        }
        
        lessonId_string = request.getParameter("lessonId");
        
        if (lessonId_string ==null)
        { 
            error_string = ErrorMessages.INVALID_ARGUMENTS;
            return;
        }
        
        try 
        {
            lessonId = Long.parseLong(lessonId_string);
        	this.courseService.removeLesson(userSession.getUserId(), lessonId);
		} 
        catch (InstanceNotFoundException e) 
        {
        	error_string = ErrorMessages.INSTANCE_NOT_FOUND;
		} 
        catch (NotAMasterOfCourseException e) 
        {
        	error_string = ErrorMessages.NOT_MASTER_COURSE;
		}
        
    }
	
}
